package org.example.skytool.security;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.Claims;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import org.example.exception.PassErrorException;
import org.springframework.util.StopWatch;

import java.util.HashMap;
import java.util.Map;

import static cn.hutool.jwt.RegisteredPayload.EXPIRES_AT;

public class JwtUtils {
    private static final String SECRET_KEY = "campus-boot-service";


    public static String getJwtToken(String userId, String openId) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("openId",openId);
        return JWTUtil.createToken(map, SECRET_KEY.getBytes());
    }

    public static Map<String, Object> parseJwtToken(String jwtToken) {
        JWT jwt = JWTUtil.parseToken(jwtToken);
        JWTPayload jwtPayload = jwt.getPayload();
        String userId = (String) jwtPayload.getClaim("userId");
        String openId = (String) jwtPayload.getClaim("openId");
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("openId",openId);
        return map;
    }

    public static boolean validateJwtToken(String jwtToken) {
        try{
            return JWTUtil.verify(jwtToken, SECRET_KEY.getBytes());
        }catch (Exception e){
            throw new PassErrorException("token错误");
        }
    }

}